package code.wang.listviewdemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import code.wang.domain.Person;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

//测试ListView
public class MainActivity extends Activity {
	
	private ListView listView ;
	private List<Person> list ; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list = new ArrayList<Person>();
		for(int i=0;i<100;i++){
			Person p = new Person();
			p.setId(i+1+"");
			p.setName("jack" + (i+1));
			p.setAge(new Random().nextInt(100) + "");
			list.add(p);
		}
		
		listView = (ListView) findViewById(R.id.lv);
		listView.setAdapter(new ListViewDemo());
		
	}
	
	class ListViewDemo extends BaseAdapter{
		
		private LayoutInflater inflater;

		public ListViewDemo() {
			super();
			inflater = LayoutInflater.from(MainActivity.this);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(convertView == null){
				convertView = inflater.inflate(R.layout.listview_item, null);
			}
			Person p = list.get(position);
			TextView tv ; 
			//序号
			tv = (TextView) convertView.findViewById(R.id.num);
			tv.setText(p.getId());
			//名称
			tv = (TextView) convertView.findViewById(R.id.name);
			tv.setText(p.getName());
			//年龄
			tv = (TextView) convertView.findViewById(R.id.age);
			tv.setText(p.getAge());
			
			return convertView;
		}
		
	}
	
	
}
